package com.zhongwang.mms.module.jiaGongStorage.controller;

import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.gen.entity.User;
import com.zhongwang.mms.module.jiaGongStorage.model.JAccountStorage;
import com.zhongwang.mms.module.jiaGongStorage.model.JAccountStorageQueryModel;
import com.zhongwang.mms.module.jiaGongStorage.model.JStorageInventory;
import com.zhongwang.mms.module.jiaGongStorage.service.StorageAccountQueryService;
import com.zhongwang.mms.module.purchase.model.PurOrderForQueryResultModel;
import com.zhongwang.mms.module.purchase.model.PurWarQueryModel;
import com.zhongwang.mms.module.purchase.service.PurWarQueryService;
import com.zhongwang.mms.util.DateUtil;
import com.zhongwang.mms.util.FileUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Description: 出入库综合查询
 * @Author: China.zh
 * @Date: 2020/4/8 10:23
 */
@Slf4j
@Controller
@RequestMapping("/jiaGongAccountQuery")
public class StorageAccountQueryController extends BaseController {

    @Autowired
    private PurWarQueryService purWarQueryService;
    @Autowired
    private StorageAccountQueryService storageAccountQueryService;

    /**
     * 页面
     *
     * @return 页面路径
     */
    @GetMapping("/page")
    public String project(Model model) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
        String start = sdf.format(new Date());
        String end = sdf2.format(new Date());
        model.addAttribute("start",start+"-1");
        model.addAttribute("end",end);
        return "jiaGongStorage/storageAccountQuery/page";
    }

    /**
     * 首页跳转页面
     *
     * @return 页面路径
     */
    @GetMapping("/index/{mode}/{totalPrice}/{year}/{month}")
    public String projectJump(Model model,@PathVariable("mode") String mode,@PathVariable("totalPrice") String totalPrice,@PathVariable("year") Integer year,@PathVariable("month") Integer month) {
        model.addAttribute("mode",mode);
        model.addAttribute("totalPrice",totalPrice);
        String start = DateUtil.getFirstDayOfMonth(month);
        start = start.replace(start.substring(0,start.indexOf("-")),year.toString());
        model.addAttribute("start",start.substring(0,10));
        String end = DateUtil.getLastDayOfMonth(month);
        end = end.replace(end.substring(0,end.indexOf("-")),year.toString());
        model.addAttribute("end",end.substring(0,10));
        return "purchase/purWarManage/purWarQuery/page";
    }

    /**
     * 操作列查看按钮
     * 跳转详细页面
     *
     * @return 页面路径
     */
    @GetMapping("/info/{id}")
    public String info(@PathVariable("id") Integer id, Model model) {
        model.addAttribute("odrId", id);
        return "jiaGongStorage/storageAccountQuery/info/page";
    }

    /**
     * 列表
     *
     * @param param 查询条件
     * @return list
     */
    @ResponseBody
    @PostMapping("/list")
    public JsonResult list(JStorageInventory param) {
        startPage();
        List<JStorageInventory> list = storageAccountQueryService.findPurWarList(param);
        return jsonResult(list);
    }

    /**
     * 列表 获取单据内容
     * @param param 查询条件
     * @return list
     */
    @ResponseBody
    @PostMapping("/findOdaListByOdrId")
    public JsonResult findOdaListByOdrId(JAccountStorageQueryModel param) {
        JAccountStorage jAccountStorage = storageAccountQueryService.findOrderInfoByOdrId(param);
        return JsonResult.success("",jAccountStorage);
    }

    /**
     * 列表 导出
     * @param param 查询条件
     * @return list
     */
    @PostMapping("/exportExcel")
    @ResponseBody
    public JsonResult exportExcel(JAccountStorageQueryModel param) {
        return storageAccountQueryService.exportExcel(param);
    }

    @Value("${user.upload.path}")
    private String uploadPath;
    /**
     * 列表 下载
     * @param excelFileName 下载的文件名
     * @return list
     */
    @GetMapping("/downloadExportExcel/{excelFileName}")
    @ResponseBody
    public void downloadExportExcel(@PathVariable("excelFileName") String excelFileName, HttpServletResponse response) {
        //获取当前用户
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        String excelPath = uploadPath + user.getAccount() + "/"+excelFileName;
        FileUtils.downloadFile(excelPath, response);
    }
}
